package cs5226.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import cs5226.dao.ConfigurationDAO;
import cs5226.model.AwrSetting;

public class ConfigurationAction extends ActionSupport implements
ServletRequestAware  {
	static Log log = LogFactory.getLog(ConfigurationAction.class);	
	
	private HttpServletRequest request = null;

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	
	public String configureAwrEntry() throws Exception {
		try {
			AwrSetting awrsetting = ConfigurationDAO.getAwrSetting();
			
			request.getSession().setAttribute("awrsetting", awrsetting);						
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
		}

		return super.SUCCESS;
	}
	
	public String configureAwrExecute() throws Exception {		
		try {
			int interval = Integer.parseInt(request.getParameter("interval"));
			int retention = Integer.parseInt(request.getParameter("retention"));
			
			log.info("Modify AWR setting: interval = " + interval + ", retention = " + retention);
			ConfigurationDAO.modifyAwrSetting(interval, retention);	
								
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
			return super.ERROR;					
		}

		return super.SUCCESS;
	}
	
	

	

}
